﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class MasterPage : System.Web.UI.MasterPage
{
    bool Log = false;

    AccesoDatos datos = new AccesoDatos();
    DataSet ds = new DataSet();

    public void CargarCarrito()
    {
        Dictionary<string, int> carrito;
        carrito = (Dictionary<string, int>)Session["Carrito"];
        if (carrito != null)
        {
            lstCompra.Items.Clear();
            foreach (KeyValuePair<string, int> fila in carrito)
            {
                ListItem registro = new ListItem(fila.Key.ToString() + " x" + fila.Value.ToString(), fila.Key.ToString());
                lstCompra.Items.Add(registro);
            }
            Session["Carrito"] = carrito;
        }
    }

    public void VaciarCarrito()
    {
        Dictionary<string, int> carrito;
        carrito = (Dictionary<string, int>)Session["Carrito"];
        if (carrito != null)
        {
            carrito.Clear();
            lstCompra.Items.Clear();
            CargarCarrito();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        datos.LlenarDataset("Select IDUsuario from Usuarios", "Usuarios", ref ds);
        if (!IsPostBack)
        {
            if (!IsPostBack) CargarCarrito();
            if(this.Session["Log"] == "1")
            {
                lblUsuario.Visible = false;
                lblClave.Visible = false;
                txtUsuario.Visible = false;
                txtClave.Visible = false;
                btnEntrar.Visible = false;
                lnkRegistrarse.Visible = false;
                lblLog.Visible = true;
                lnkSalir.Visible = true;
                lblUser.Text = this.Session["usuario"].ToString();
                lblUser.Visible = true;
            }
        }
    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        this.Server.Transfer("Home.aspx");
    }
    protected void btnBebidas_Click(object sender, EventArgs e)
    {
        this.Server.Transfer("Bebidas.aspx");
    }
    protected void btnPromociones_Click(object sender, EventArgs e)
    {
        this.Server.Transfer("Promociones.aspx");
    }
    protected void btnTragos_Click(object sender, EventArgs e)
    {
        this.Server.Transfer("Tragos.aspx");
    }
    protected void btnComprar_Click(object sender, EventArgs e)
    {
        if (this.Session["Log"] == "1") this.Server.Transfer("Compras.aspx");
        else Page.ClientScript.RegisterStartupScript(this.GetType(), "Error de Login", "<script type = \"text/javascript\">alert('" + "Es necesario estar logueado para continuar con la compra" + "');</script>");        
    }
    protected void lnkRegistrarse_Click(object sender, EventArgs e)
    {
        this.Server.Transfer("Registro.aspx");
    }
    protected void btnEntrar_Click(object sender, EventArgs e)
    {
        GestionUsuarios gusuarios = new GestionUsuarios();
        if (gusuarios.Entrar(txtUsuario.Text,txtClave.Text))
        {
            Log = true;
            this.Session["Log"] = "1";
            this.Session["usuario"] = txtUsuario.Text;
            this.Response.Redirect("Home.aspx");
        }
        else Page.ClientScript.RegisterStartupScript(this.GetType(), "Error de Login", "<script type = \"text/javascript\">alert('" + "Usuario o Clave Incorrecta" + "');</script>");
    }
    protected void lnkSalir_Click(object sender, EventArgs e)
    {
        Log = true;
        this.Session["Log"] = "0";
        lblUsuario.Visible = true;
        lblClave.Visible = true;
        txtUsuario.Visible = true;
        txtClave.Visible = true;
        btnEntrar.Visible = true;
        lnkRegistrarse.Visible = true;
        lblLog.Visible = false;
        lnkSalir.Visible = false;
        lblUser.Visible = false;
        Dictionary<string, int> carrito;
        carrito = (Dictionary<string, int>)Session["Carrito"];
        carrito.Clear();
        Session["Carrito"] = carrito;
        lstCompra.Items.Clear();
        CargarCarrito();
    }

    protected void btnQuitar_Click(object sender, EventArgs e)
    {
        //lstCompra.Items.Remove(lstCompra.Items[lstCompra.SelectedIndex]);
        Dictionary<string, int> carrito;
        carrito = (Dictionary<string, int>)Session["Carrito"];
        if (carrito != null && lstCompra.SelectedIndex!=-1)
        {
            string producto = lstCompra.Items[lstCompra.SelectedIndex].Value.ToString();
            carrito.Remove(producto);
            Session["Carrito"] = carrito;
            lstCompra.Items.Remove(lstCompra.Items[lstCompra.SelectedIndex]);
            CargarCarrito();
        }
    }

    protected void btnQuitarTodo_Click(object sender, EventArgs e)
    {
        VaciarCarrito();
    }
}
